<?php
/**
 * 海创管理平台所有普通类的父类
 * @author sz 2016-9-12 10:01:42
 * @version 1.0
 */
namespace Home\Controller;
use Think\Controller;
import('ORG.Util.Page'); // 导入分页类
class PmpController extends Controller
{

    /**
     * 定义操作类型添加常量对应值
     *
     * @var char
     */
    protected $operationAdd = '0';

    /**
     * 定义操作类型删除常量对应值
     *
     * @var char
     */
    protected $operationDelete = '1';

    /**
     * 定义操作类型修改常量对应值
     *
     * @var char
     */
    protected $operationModify = '2';

    /**
     * 空操作，当找不到Controller里的方法时，执行此方法
     *
     * @author sz 2016-9-12 10:03:38
     * @version 1.0
     */
    public function _empty ()
    {
        // 转向Empty的Controller中index方法
        R('Empty/index');
    }

    /**
     * 初始化设置字符编码，并验证Controller层权限
     *
     * @author sz 2016-9-12 10:17:04
     * @version 1.0
     */
    function _initialize ()
    {
        header("Content-Type:text/html; charset=utf-8");
        $ControllerName = CONTROLLER_NAME;
        
        // 如果直接访问此Controller的公共方法，跳过权限验证
        if ($ControllerName == 'pmp')
        {
            return;
        }
        if ($ControllerName != 'Login')
        {
            // 如果未登录，提示跳转到登录页面
            if (session('?hb_think_id') == false)
            {
                echo "<script type='text/javascript'>alert('您还没有登录？离开太久了？点击“确定”跳转到登录页面');window.parent.location.href='?c=login&a=index';</script>";
                exit();
            }
            else
            {
                $group_id = $_SESSION['hb_think_group_id'];
                
                $auth_parent_id = 0;
                // 判断是否是超级管理员，不是超级管理员需要验证
                if ($group_id != '2')
                {
                    $controllerName = strtolower(CONTROLLER_NAME . "/" . ACTION_NAME);
                    $authInfo = D('AuthInfo')->getAuthByName($controllerName);
                    // 判断数据表里面是否有该模板，没有的话直接没有权限反问
                    if ($authInfo != null)
                    {
                        $auth_parent_id = $authInfo[0]['parent_id'];
                        $authFLag = $this->getAuthById($group_id, $authInfo[0]['id']);
                        // 判断该分组是否有此模板的访问权限
                        if ($authFLag == false)
                        {
                            echo "<script type='text/javascript'>alert('您还没有访问权限？点击“确定”跳转到首页');window.parent.location.href='?c=index&a=index';</script>";
                            exit();
                        }
                    }
                    // 隐藏的菜单
                    foreach (session('auth_arr') as $auth)
                    {
                        $auth_ids = $auth['auth_ids'];
                        
                        // 根据权限id去查询数据
                        $authArray = D('AuthInfo')->getAuthByGroupId($auth_ids);
                        $colltrollerStr = "";
                        foreach ($authArray as $key => $value)
                        {
                            if ($key == (count($authArray) - 1))
                            {
                                $colltrollerStr .= $value['controller_name'];
                            }
                            else
                            {
                                $colltrollerStr .= $value['controller_name'] . ",";
                            }
                        }
                        C('CONTROLLER_NAME', $colltrollerStr);
                    }
                    // 隐藏标签
                    $authInfo = D('AuthInfo')->getAuthById($auth_parent_id);
                    $authStr = "";
                    foreach ($authInfo as $key => $value)
                    {
                        $flag = $this->getAuthById($group_id, $value['id']);
                        if ($flag == false)
                        {
                            $authStr .= $value['oprate_name'] . ",";
                        }
                    }
                    $authStr = substr($authStr, 0, (strlen($authStr) - 1));
                    C('AUTH_NAME', $authStr);
                }
            }
        }
        $phpself = $_SERVER['REQUEST_URI'];
        C('ADMIN_ID', $_SESSION['hb_think_id']);
        C('ADMIN_NAME', $_SESSION['hb_think_name']);
        C('GROUP_ID', $_SESSION['hb_think_group_id']);
        C('ADMIN_GROUP_NAME', $_SESSION['hb_think_group_name']);
        C('ADMIN_AVATAR', $_SESSION['hb_think_avatar']);
    }

    /**
     * 记录日志
     *
     * @param array $logArray            
     *
     * @author sz 2016-9-12 10:05:52
     * @version 1.0
     */
    public function addLog ($logArray)
    {
        $logModel = D('LogInfo');
        $user_id = $_SESSION['hb_think_id'];
        // 遍历日志信息数组，记录日志消息
        foreach ($logArray as $key => $sqlArray)
        {
            foreach ($sqlArray as $behavior)
            {
                $logModel->addLog($user_id, $key, $behavior);
            }
        }
    }

    /**
     * mysql查询关键字过滤
     *
     * @param String $keyword
     *            关键词
     * @return 返回特殊字符替换后的关键词
     * @author sz 2015-12-11 14:18:48
     * @version 1.0
     */
    public function mysqlKeyword ($keyword)
    {
        return str_replace(array(
                "\\",
                '%',
                "_",
                "'"
        ), array(
                "\\\\",
                '\%',
                '\_',
                "\\'"
        ), $keyword);
    }

    /**
     * 验证码
     *
     * @author sz 2015-12-16 15:13:30
     * @version 1.0
     */
    public function getCharAndNumr ($length)
    {
        $returnStr = '';
        $pattern = '1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLOMNOPQRSTUVWXYZ';
        for ($i = 0; $i < $length; $i ++)
        {
            $returnStr .= $pattern{mt_rand(0, 61)}; // 生成php随机数
        }
        return $returnStr;
    }

    /**
     * 得到指定日期相差的天数
     * 不要小数点
     * 
     * @return string
     */
    function diffBetweenTwoMonthOther ($day1, $day2)
    {
        $second1 = strtotime($day1);
        $second2 = strtotime($day2);
        
        if ($second1 < $second2)
        {
            $tmp = $second2;
            $second2 = $second1;
            $second1 = $tmp;
        }
        $tepDate = ($second1 - $second2) / 86400;
        return floor(($tepDate / 30));
    }

    /**
     * 根据id判断是否有权限
     *
     * @param int $group_id
     *            组id
     * @author sz
     * @version 1.0
     */
    public function getAuthById ($group_id, $auth_id)
    {
        if (session('?auth_arr') == false)
        {
            // 获取所有编辑信息并组织为键值对形式
            $authArray = D('GroupInfo')->getGroupById($group_id);
            session('auth_arr', $authArray);
        }
        // 判断session中是否已有原先的值
        $auth_flag = false;
        foreach (session('auth_arr') as $auth)
        {
            $tag = explode(',', $auth['auth_ids']);
            if (in_array($auth_id, $tag))
            {
                $auth_flag = true;
            }
        }
        
        return $auth_flag;
    }

    /**
     * CURL获取远程图片大小
     * -----------------------------------------------------------------
     * 
     * @author sz
     * @version 1.0
     */
    function remote_filesize ($uri, $user = '', $pw = '')
    {
        ob_start();
        $ch = curl_init($uri);
        curl_setopt($ch, CURLOPT_HEADER, 1);
        curl_setopt($ch, CURLOPT_NOBODY, 1);
        if (! empty($user) && ! empty($pw))
        {
            $headers = array(
                    'Authorization: Basic ' . base64_encode($user . ':' . $pw)
            );
            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        }
        $okay = curl_exec($ch);
        curl_close($ch);
        $head = ob_get_contents();
        ob_end_clean();
        $regex = '/Content-Length:\s([0-9].+?)\s/';
        $count = preg_match($regex, $head, $matches);
        return isset($matches[1]) ? $matches[1] : 'unknown';
    }
    
    /**
     * CURL获取远程图片尺寸
     *
     * @access private
     * @param  str $src
     * @return array
     * @author sz
     * @version 1.0
     */
    function get_img_dim( $uri )
    {
        $headers = array( 'Range: bytes=0-' . $this->range );
         
        $curl = curl_init( $uri );
        curl_setopt( $curl, CURLOPT_SSL_VERIFYPEER, false );
        curl_setopt( $curl, CURLOPT_HTTPHEADER, $headers );
        curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true );
        $data = curl_exec( $curl );
        curl_close( $curl );
         
        $img = imagecreatefromstring( $data );
        $colors = imagecolorstotal( $img );
        $width = imagesx( $img );
        $height = imagesy( $img );
         
        $img_dim = array( $width, $height, $colors );
         
        imagedestroy( $img );
         
        return $img_dim;
    }
}
?>